package homework;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @author DCSGO
 * @version 1.0
 * 要求:使用BufferedReader读取一个文本文件
 * 为每行加上行号,再连同内容一并输出到屏幕上。
 */
public class Homework02 {
    //正常读取 UTF-8
    @Test
    public void read1() {
        String filePath = "src\\readme.java";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filePath));
            int lineNum = 0;
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println((++lineNum) + "\t" + line);
            }
        } catch (IOException e) {
            e.getStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                    System.out.println("----------------数据读取完毕-------------");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //读取非 UTF-8 的文本文件
    @Test
    public void read2() {//GBK
        String filePath = "src\\transformation\\ANSI.txt";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "GBK"));
            int lineNum = 0;
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println((++lineNum) + "\t" + line);
            }
        } catch (IOException e) {
            e.getStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.getStackTrace();
            }
        }
    }
}
